package org.jeecg.modules.tt_question_collect.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.tt_question_collect.entity.TtCollectFlowNode;
import org.jeecg.modules.tt_question_collect.service.ITtAuditRecordService;
import org.jeecg.modules.tt_question_collect.service.ITtCollectFlowNodeService;
import org.jeecg.modules.tt_question_collect.util.TransUntil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @Description: 审批流程节点表
 * @Author: jeecg-boot
 * @Date: 2021-02-02
 * @Version: V1.0
 */
@Api(tags = "审批流程节点表")
@RestController
@RequestMapping("/tt_question_collect/ttCollectFlowNode")
@Slf4j
public class TtCollectFlowNodeController extends JeecgController<TtCollectFlowNode, ITtCollectFlowNodeService> {
    @Autowired
    private ITtCollectFlowNodeService ttCollectFlowNodeService;

    @Autowired
    private ITtAuditRecordService ttCollectFlowService;

    /**
     * 对应问题审批流程节点列表查询
     *
     * @param ttCollectFlowNode
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */

    @AutoLog(value = "对应问题审批流程节点列表查询")
    @ApiOperation(value = "对应问题审批流程节点列表查询", notes = "对应问题审批流程节点列表查询")
    @GetMapping(value = "/list")
    public Result<List<Map<String, Object>>> queryPageList(TtCollectFlowNode ttCollectFlowNode,String scbj,String lclx ,
                                                           @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                           @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {

        List<Map<String, Object>> listmap = new ArrayList<Map<String, Object>>();
        List<Map<String, Object>> list = ttCollectFlowService.queryCollectFlowSList(ttCollectFlowNode.getId(), scbj,lclx);
        // 节点列表
        list = TransUntil.transformLowerCase(list);
        // maxsortid，代表为共有多少级审核节点
        int maxsortid = ttCollectFlowNodeService.maxSortidByid(ttCollectFlowNode.getId(),lclx);
        //int maxsortid = ttCollectFlowNodeService.maxSortidBycode(ttCollectFlowNode.getFlowcode());
        // 把每条数据插入到对应的sortid数组里,循环几次就是有多少级
        for (int i = 1; i < maxsortid + 1; i++) {

            Map<String, Object> map = new HashMap<String, Object>();
            List<Map<String, Object>> nodeList = new ArrayList<>();
            // 循环当前有多条此流程的节点数据
            for (int j = 0; j < list.size(); j++) {
                // 循环得到此流程每一条节点数据
                // 如果得到的节点数据为对应的循环节点相同，那么加入;
                if (Integer.parseInt(list.get(j).get("sortid").toString()) == i) {
                    nodeList.add(list.get(j));
                }
            }
            map.put("jsh" + i, nodeList);
            listmap.add(map);
        }
        System.out.println(com.alibaba.fastjson.JSON.toJSONString(listmap));
        return Result.OK(listmap);
    }

    /**
     * 流程设置流程图节点设置列表
     *
     * @param ttCollectFlowNode
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "流程设置流程图节点设置列表")
    @ApiOperation(value = "流程设置流程图节点设置列表", notes = "流程设置流程图节点设置列表")
    @GetMapping(value = "/lcszjdlist")
    public Result<List<Map<String, Object>>> queryPagelcszList(TtCollectFlowNode ttCollectFlowNode,
                                                               @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                               @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {

        List<Map<String, Object>> listmap = new ArrayList<Map<String, Object>>();
        List<Map<String, Object>> list = ttCollectFlowNodeService
                .queryCollectNodesList(ttCollectFlowNode.getFlowcode());

        // 节点列表
        list = TransUntil.transformLowerCase(list);

        // maxsortid，代表为共有多少级审核节点
        int maxsortid = ttCollectFlowNodeService.maxSortidBycode(ttCollectFlowNode.getFlowcode());

        // 把每条数据插入到对应的sortid数组里,循环几次就是有多少级
        for (int i = 1; i < maxsortid + 1; i++) {

            Map<String, Object> map = new HashMap<String, Object>();
            List<Map<String, Object>> nodeList = new ArrayList<>();
            // 循环当前有多条此流程的节点数据
            for (int j = 0; j < list.size(); j++) {
                // 循环得到此流程每一条节点数据
                // 如果得到的节点数据为对应的循环节点相同，那么加入;
                if (Integer.parseInt(list.get(j).get("sortid").toString()) == i) {
                    nodeList.add(list.get(j));
                }
            }

            map.put("jsh" + i, nodeList);
            listmap.add(map);
        }
        System.out.println(com.alibaba.fastjson.JSON.toJSONString(listmap));
        return Result.OK(listmap);
    }

    /**
     * 添加
     *
     * @param ttCollectFlowNode
     * @return
     */
    @AutoLog(value = "审批流程节点表-添加")
    @ApiOperation(value = "审批流程节点表-添加", notes = "审批流程节点表-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody TtCollectFlowNode ttCollectFlowNode) {
        // 获取人员信息
        List<Map<String, Object>> listuser = ttCollectFlowNodeService.ids(ttCollectFlowNode.getAudittype(), ttCollectFlowNode.getBy2());

        // 增加当前节点记录
        if (null == listuser && listuser.isEmpty()) {
            return Result.error("添加失败,审核人员不存在！");
        }
        else {
            listuser = TransUntil.transformLowerCase(listuser);
            // 传入 当前节点sortid和当前流程编码，添加上级传tjsj，添加下级传tjxj
            return (ttCollectFlowNodeService.insertId(listuser, ttCollectFlowNode));
        }
    }

    /**
     * 编辑
     *
     * @param ttCollectFlowNode
     * @return
     */
    @AutoLog(value = "审批流程节点表-编辑")
    @ApiOperation(value = "审批流程节点表-编辑", notes = "审批流程节点表-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody TtCollectFlowNode ttCollectFlowNode) {
        // 获取人员信息
        List<Map<String, Object>> listuser = ttCollectFlowNodeService.ids(ttCollectFlowNode.getAudittype(), ttCollectFlowNode.getBy2());
        // 增加当前节点记录
        if (null == listuser && listuser.isEmpty()) {
            return Result.error("失败,审核人员不存在！");
        }
        listuser = TransUntil.transformLowerCase(listuser);
        ttCollectFlowNodeService.updatebyId(listuser, ttCollectFlowNode);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "审批流程节点表-通过id删除")
    @ApiOperation(value = "审批流程节点表-通过id删除", notes = "审批流程节点表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id,
                            @RequestParam(name = "sortid", required = true) String sortid) {
        // 此节点以下所有节点增加一级
        return ttCollectFlowNodeService.updatesortid(sortid, id);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "审批流程节点表-批量删除")
    @ApiOperation(value = "审批流程节点表-批量删除", notes = "审批流程节点表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.ttCollectFlowNodeService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "审批流程节点表-通过id查询")
    @ApiOperation(value = "审批流程节点表-通过id查询", notes = "审批流程节点表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        TtCollectFlowNode ttCollectFlowNode = ttCollectFlowNodeService.getById(id);
        if (ttCollectFlowNode == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(ttCollectFlowNode);
    }

    /**
     * 导出excel
     *
     * @param request
     * @param ttCollectFlowNode
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, TtCollectFlowNode ttCollectFlowNode) {
        return super.exportXls(request, ttCollectFlowNode, TtCollectFlowNode.class, "审批流程节点表");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, TtCollectFlowNode.class);
    }

    /**
     * 提交流程
     *
     * @param id
     * @param flowCode
     * @return
     */
    @AutoLog(value = "审批流程节点表-提交审批流程")
    @ApiOperation(value = "审批流程节点表-提交审批流程", notes = "审批流程节点表-提交审批流程")
    @PostMapping(value = "/submitProcess")
    public Result<?> submitProcess(@RequestParam(name = "id", required = true) String id,
                                   @RequestParam(name = "flowCode", required = true) String flowCode,

                                  String lclx) {


      //  System.out.println(proname);

  //    String flowCode =   ttCollectFlowService.getflowid(proname);

        if(lclx.isEmpty()){
            return Result.error("lclx参数不可为空!");
        }
    	if ("falc".equals(lclx)) {
    		String facode = ttCollectFlowNodeService.getfacode();
    		if (facode.isEmpty()) {
				return Result.error("方案审批code未找到！");
			}else {
				return ttCollectFlowNodeService.submitProcess(id, facode,lclx);
			}
		}else {
			return ttCollectFlowNodeService.submitProcess(id, flowCode,lclx);
        }

    }

}
